
export default function Promise(excuter) {
    let _this = this;
    this.status = "pending"
    this.data = undefined
    this.callback = []
    // resolve 执行的时候 携带value参数去触发then里的回调函数
    function resolve(value) {
        // 避免重复调用
        if (_this.status !== "pending") return
        // 更改状态为resolve
        _this.status = "resolve"
        // 如果有回调函数，立即执行
        if (_this.callback.length > 0) {
            _this.callback.forEach(cb => {
                setTimeout(() => { // 放入队列中执行所有的回调
                    cb.onResolve(value)
                }, 100);
            });
        }
    }
    function reject(value) {
        // 避免重复调用
        if (_this.status !== "pending") return
        // 更改状态为resolve
        _this.status = "reject"
        // 如果有回调函数，立即执行
        if (_this.callback.length > 0) {
            _this.callback.forEach(cb => {
                setTimeout(() => { // 放入队列中执行所有的回调
                    cb.onReject(value)
                }, 100);
            });
        }
    }

    excuter(resolve, reject)

    Promise.prototype.then = (onResolve, onReject) => {
        this.callback.push({
            onResolve,
            onReject
        })
    }

    Promise.prototype.catch = (reject) => {
        this.callback.push({
            onReject
        })
    }

    Promise.all = (primsies) => {

    }

    Promise.race = () => {

    }


}

